package com.taotao.feign.config;

import com.taotao.feign.proxy.FeignClientProxy;
import com.taotao.feign.proxy.SwFeignInvocationHandler;
import com.taotao.feign.utils.PackageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
    //实现接口获取配置文件内容、
    //ImportBeanDefinitionRegistrar 当springIOC 容器初始化 优先注册 自己创建bean 对象
    private Environment environment;


    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {

    }


    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        //那些报下面接口需要注册
        try {
            Map<String, String> feignService = environment.getProperty("sw.cloud.feign-service", Map.class);

            String pk = "com.taotao.service";
            //通过反射获取该保下所有的接口
            List<String> classname = PackageUtil.getClassName(pk);
            //类才能注册到IOC容器
            for (int i = 0; i < classname.size(); i++) {
                //根据接口生成代理类
                String swInterance = classname.get(i);
                Class<?> aClass = Class.forName(swInterance);//基于接口形式生成动态代理

                Object proxyClass = FeignClientProxy.create(aClass);
                //4 将代理类注册到ioc容器
                ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
                constructorArgumentValues.addIndexedArgumentValue(0, new SwFeignInvocationHandler());

                //将代理类注册到ioc
                BeanDefinition definition = new RootBeanDefinition(proxyClass.getClass(), constructorArgumentValues, null);
                String beanName = aClass.getSimpleName();
                BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, beanName, null);
                BeanDefinitionReaderUtils.registerBeanDefinition(holder, beanDefinitionRegistry);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
